package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.interceptor.LoginRequire;
import cn.wolfcode.luowowo.interceptor.ResolverUserInfo;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.redis.StrategyStatisVO;
import cn.wolfcode.luowowo.redis.srevice.IStrategyRedisSrevice;
import cn.wolfcode.luowowo.redis.srevice.IUserRedisSrevice;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import com.alibaba.fastjson.JSON;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 攻略明细界面
 */
@RestController
@RequestMapping("/strategies")
public class strategiesController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    
    @Autowired
    private IUserRedisSrevice redisSrevice;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    //统计数据的服务层
    @Autowired
    private IStrategyRedisSrevice strategyRedisSrevice;
    
    //查询目的地下的攻略明细内容
    @GetMapping("detail")
    public Object detail(String id) {
        //根据攻略的id查询攻略的明细内容
        Strategy strategy = strategyService.get(id);
        
        //每次刷新页面就会使用这个方法一次,所以也就是说他代表可以用来增加攻略的阅读数
        strategyRedisSrevice.increaseStrategyStatis(id, 1);
        
        return JsonResult.success(strategy);
    }
    
    //查询所有的攻略主题
    @GetMapping("/themes")
    public Object themes() {
        List<StrategyTheme> list = strategyThemeService.list();
        return JsonResult.success(list);
    }
    
    //获取所有的攻略,进行分页
    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        Page<Strategy> query = strategyService.query(qo);
        return JsonResult.success(query);
    }
    
    /**
     * 根据攻略信息获取评论的信息
     *
     * @param qo 评论分页信息
     * @return 返回的是分页后的评论数据
     */
    @GetMapping("/comments")
    public Object query(StrategyCommentQuery qo, @ResolverUserInfo UserInfo userInfo) {
        Page<StrategyComment> query = strategyCommentService.query(qo);
        return JsonResult.success(query);
    }
    
    /**
     * 增加一个评论
     *
     * @return
     */
    @LoginRequire
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, @ResolverUserInfo UserInfo userInfo) {
        //copy方法 第一个参数是源数据 第二个参数是目标数据 匹配两个数据源里面同名的字段,将源字段的内容拷贝到目标数据的字段上
        BeanUtils.copyProperties(userInfo, strategyComment);
        //因为源数据上面的是user的id是对不上评论中的userId 所以这里要单独设置
        strategyComment.setUserId(userInfo.getId());
        //增加评论统计的数量
        strategyRedisSrevice.increaseStrategyCommentStatis(strategyComment.getStrategyId());
        strategyCommentService.save(strategyComment);
        return JsonResult.success();
    }
    
    /**
     * 点赞功能
     *
     * @param cid      评论的id
     * @param userInfo 登录用户的信息
     * @return
     */
    @LoginRequire
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, @ResolverUserInfo UserInfo userInfo) {
        //将登录的用户id和评论id带到业务层进行判断
        strategyCommentService.commentThumb(cid, userInfo.getId());
        
        return JsonResult.success();
    }
    
    /**
     * 查询攻略的阅读数等统计数据
     *
     * @param sid 攻略的id
     * @return
     */
    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        //将攻略id传到后端去获取指定攻略的vo对象
        StrategyStatisVO vo = strategyRedisSrevice.getStrategyStatis(sid);
        //将vo对象返回
        return JsonResult.success(vo);
    }
    
    /**
     * 收藏逻辑
     *
     * @param sid      攻略的id
     * @param userInfo 登录用户的信息
     * @return
     */
    @LoginRequire
    @PostMapping("/favor")
    public Object favor(String sid, @ResolverUserInfo UserInfo userInfo) {
        /**
         * true:收藏成功
         * false:取消收藏成功
         */
        Boolean rtf = strategyRedisSrevice.strategyFavornumStatis(sid, userInfo.getId());
        //前端需要返回的是一个boolean值
        return JsonResult.success(rtf);
    }
    //用户顶当前攻略的数据,前台没有传用户的id  根据参数解析器拦截一个
    @LoginRequire
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@ResolverUserInfo UserInfo userInfo){
        //返回的是一个boolean.
        /**
         * true:代表顶成功了
         * false:代表今天已经顶过了
         */
        Boolean ret = strategyRedisSrevice.saveThumbsupnum(sid,userInfo.getId());
        return JsonResult.success(ret);
    }
    
   /* //分享攻略到微博的功能
    @LoginRequire
    @PostMapping("/weibo")
    public Object weibo(String sid){
        //点击微博的按钮进入分享状态后就分享一次
        boolean ret = strategyRedisSrevice.saveSharenum(sid);
        return JsonResult.success(ret);
    }*/
}
